package org.hefan.order.controller;

import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.LoginContextUtil;
import org.hefan.basic.util.PageList;
import org.hefan.order.domain.AdoptOrder;
import org.hefan.order.query.AdoptOrderQuery;
import org.hefan.order.service.IAdoptOrderService;
import org.hefan.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        try {
            String payData = adoptOrderService.adoptOrderSubmit(params, request);
            //System.out.println(payData);
            //将支付数据响应给前端 - 前端可以跳转到支付页面完成支付
            return AjaxResult.me().setResultObj(payData);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("下单失败！" + e.getMessage());
        }
    }

    /**
     * 管理员：管理员查询订单接口 - 平台管理员所有店铺的所有订单，店铺管理员查询当前店铺的所有订单
     * @param adoptOrderQuery
     * @param request
     * @return
     */
    @PostMapping("/admin")
    public PageList<AdoptOrder> adminAdoptOrder(@RequestBody AdoptOrderQuery adoptOrderQuery, HttpServletRequest request) {
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Long logininfoId = logininfo.getId();
        return adoptOrderService.queryAdminOrder(adoptOrderQuery, logininfoId);
    }

    /**
     * 管理员：管理员查询订单接口 - 平台管理员所有店铺的所有订单，店铺管理员查询当前店铺的所有订单
     * @param adoptOrderQuery
     * @param request
     * @return
     */
    @PostMapping("/user")
    public PageList<AdoptOrder> userAdoptOrder(@RequestBody AdoptOrderQuery adoptOrderQuery, HttpServletRequest request) {
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Long logininfoId = logininfo.getId();
        return adoptOrderService.queryUserOrder(adoptOrderQuery, logininfoId);
    }
}
